<template>
  <div class="white">
    <stateTable :headerData="headerData" :tableData="tableData" :isindex="true">
      <!-- 输入框 -->
      <template v-slot:formSlot>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="字典名称：">
            <el-input
              v-model="formInline.user"
              placeholder="请输入字典名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="字典编号：">
            <el-input
              v-model="formInline.region"
              placeholder="请输入字典编号"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSubmit()"
              >查询</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-refresh-right"
              @click="soSubmits()"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </template>

      <!-- 操作按钮 -->
      <template v-slot:btnSlot>
        <div>
          <el-button type="primary" @click="editData(1)" icon="el-icon-plus"
            >添加</el-button
          >
          <el-button type="primary" icon="el-icon-download">导出</el-button>
          <el-button type="primary" icon="el-icon-upload2">导入</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="refresh()"
            >刷新缓存</el-button
          >
          <el-button type="primary" icon="el-icon-delete" @click="dialogTab()"
            >回收站</el-button
          >
          <el-dialog :visible.sync="dialogTableVisible">
            <recycle
              @categoryListTreeSearch="categoryListTreeSearch"
              ref="recycle"
            ></recycle
          ></el-dialog>
        </div>
      </template>

      <template v-slot:operationBtn="{ operationData }">
        <div>
          <div>
            <el-button
              type="text"
              @click="editData(operationData)"
              icon="el-icon-edit"
              >编辑</el-button
            >
            <el-dialog
              title="编辑"
              :visible.sync="dialogFormVisible"
              :modal="false"
            >
              <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                <el-form-item
                  label="字典名称"
                  :label-width="formLabelWidth"
                  prop="Lname"
                >
                  <el-input
                    v-model="ruleForm.Lname"
                    autocomplete="off"
                    @input="change()"
                  ></el-input>
                </el-form-item>
                <el-form-item
                  label="字典编码"
                  :label-width="formLabelWidth"
                  prop="names"
                >
                  <el-input
                    v-model="ruleForm.names"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWidth">
                  <el-input
                    v-model="ruleForm.namew"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="compileS('ruleForm')"
                  >确 定</el-button
                >
              </div>
            </el-dialog>
            <span class="pan">|</span>
            <el-button
              icon="el-icon-setting"
              type="text"
              style="margin-left: 16px"
              @click="keep(operationData)"
            >
              字典配置
            </el-button>
            <span class="pan">|</span>

            <!-- //删除数据 -->
            <el-popconfirm
              confirm-button-text="确认"
              cancel-button-text="取消"
              icon="el-icon-info"
              content="Top Center"
              icon-color="#faad14"
              title="确认删除吗？"
              @onConfirm="deletesList(operationData)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </div>
        </div>
        <el-drawer
          title="字典名称"
          :visible.sync="drawer"
          size="600px"
          :modal="false"
        >
          <div class="bor"></div>
          <div class="borders">
            <el-form
              :inline="true"
              :model="dataList"
              class="demo-ruleForm"
              size="mini"
            >
              <el-row>
                <el-col :span="9">
                  <el-form-item label="名称:">
                    <el-input
                      v-model="dataList.itemText"
                      placeholder="请输入名称"
                      style="width: 120px"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="9">
                  <el-form-item label="状态:">
                    <el-select
                      v-model="dataList.itemValue"
                      size="mini"
                      style="width: 120px"
                    >
                      <el-option label="正常" value="1"></el-option>
                      <el-option label="禁用" value="0"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item>
                    <el-button type="primary" size="mini" @click="seek()"
                      >查询</el-button
                    >
                    <el-button type="primary" size="mini" @click="reset()"
                      >重置</el-button
                    >
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-button type="primary" @click="cook(1)" size="mini"
                    >新增</el-button
                  >
                </el-col>
              </el-row>
            </el-form>

            <!-- 新增弹框 -->
            <el-dialog
              :title="titee"
              :visible.sync="dialogFormVisibles"
              :modal="false"
            >
              <el-form :model="form" :rules="rules" ref="form">
                <el-form-item
                  label="名称："
                  prop="nameA"
                  :label-width="formLabelWidth"
                >
                  <el-input
                    v-model="form.nameMe"
                    autocomplete="off"
                    size="mini"
                    placeholder="请输入名称"
                  ></el-input>
                </el-form-item>
                <el-form-item
                  label="数据值："
                  prop="nameS"
                  :label-width="formLabelWidth"
                >
                  <el-input
                    v-model="form.nameS"
                    autocomplete="off"
                    size="mini"
                    placeholder="请输入数据值"
                  ></el-input>
                </el-form-item>
                <el-form-item label="描述：" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.nameD"
                    autocomplete="off"
                    size="mini"
                  ></el-input>
                </el-form-item>
                <el-form-item label="排序值：" :label-width="formLabelWidth">
                  <el-input-number
                    v-model="form.num"
                    style="width: 120px"
                    size="mini"
                    controls-position="right"
                  ></el-input-number>
                  值越小靠前，支持小数
                </el-form-item>
                <el-form-item label="是否启用：" :label-width="formLabelWidth">
                  <el-switch
                    v-model="form.value"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                  >
                  </el-switch>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisibles = false">取 消</el-button>
                <el-button type="primary" @click="submit('form')"
                  >确 定</el-button
                >
              </div>
            </el-dialog>

            <!-- 字典配置抽屉 -->
            <el-table style="width: 100%" :data="tableDataA">
              <el-table-column prop="itemText" label="名称" width="180">
              </el-table-column>
              <el-table-column prop="itemValue" label="数据值" width="180">
              </el-table-column>
              <el-table-column prop="address" label="操作">
                <template slot-scope="props">
                  <el-button type="text" @click="cook(props.row)"
                    >编辑</el-button
                  >
                  <span class="pan">|</span>
                  <el-popconfirm
                    confirm-button-text="确认"
                    cancel-button-text="取消"
                    icon="el-icon-info"
                    content="Top Center"
                    icon-color="#faad14"
                    title="确认删除吗？"
                    @onConfirm="ExpurgateSearch(props.row)"
                  >
                    <el-button slot="reference" type="text">删除</el-button>
                  </el-popconfirm>
                </template>
              </el-table-column>
            </el-table>
            <div class="block">
              <el-pagination
                @size-change="handleSize"
                @current-change="handleCurrent"
                :current-page="pageNo"
                :page-sizes="[10, 20, 30]"
                :page-size="firstsize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </div>
          </div>
        </el-drawer>
      </template>
    </stateTable>
    <div></div>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="[10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import stateTable from "@/components/xiaohei/stateTable.vue";
import recycle from "@/components/xiaohei/recycle.vue";
import {
  categoryListTree,
  addition,
  compile,
  deletes,
  dicitemList,
  newlyIncreased,
  Editor,
  Expurgate,
} from "@/api/dict"
export default {
  name: "dict",
  data() {
    return {
      dialogFormVisibles: false,
      addId: "",
      drawer: false,
      dialogTableVisible: false,
      visible: false,
      edit_OR_add: "",
      operationData1: [],
      message1: "",
      dialogcompile: false,
      message: "",
      dialogFormVisible: false,
      pageNo: 1,
      pageSize: 10,
      firstNO: 1,
      firstsize: 10,
      operation: [],
      tableDataA: [],
      tableData: [],
      abc: {},
      headerData: [
        // {
        //   // prop: "id",
        //   label: "#",
        //   // width: "180"
        // },
        {
          prop: "dictName",
          label: "字典名称",
          // width: "180",
        },
        {
          prop: "dictCode",
          label: "字典编号",
          // width: "180"
        },
        {
          prop: "description",
          label: "描述",
          // width: "180"
        },
        // {
        //   prop: "compile",
        //   label: "编辑",
        //   width: "180",
        //   slot: true,
        // },
      ],
      dataList: {
        itemText: "",
        itemValue: "",
      },
      form: {
        value: true,
        nameMe: "",
        nameS: "",
        nameD: "",
        num: 1,
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      ruleForm: {
        names: "",
        namew: "",
        Lname: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      rules: {
        Lname: [
          { required: true, message: "请输入字典名称！", trigger: "blur" },
          {
            min: 3,
            max: 100,
            message: "长度在 3 到 100 个字符",
            trigger: "blur",
          },
        ],
        names: [
          { required: true, message: "请输入字典编码！", trigger: "blur" },
          {
            min: 3,
            max: 100,
            message: "长度在 3 到 100 个字符",
            trigger: "blur",
          },
        ],
        nameMe: [
          { required: true, message: "请输入名称！", trigger: "change" },
        ],
        nameS: [
          { required: true, message: "请输入数据值！", trigger: "change" },
        ],
      },
      formLabelWidth: "120px",
      formInline: {
        user: "",
        region: "",
      },
    };
  },
  methods: {
    // 字典刷新
    refresh() {
      this.categoryListTreeSearch();
      this.$message({
        showClose: true,
        message: "刷新缓存成功",
        type: "success",
      });
    },
    // 字典配置删除
    ExpurgateSearch(props) {
      console.log(props);
      let _self = this;
      let id = props.id;
      Expurgate(id).then((res) => {
        console.log(res);
        if (res.code == 200) {
          _self.dicitemListTab();
          _self.$message({
            showClose: true,
            message: res.message,
            type: "success",
          });
        }
      });
    },
    // 编辑和新增弹框点击
    cook(val) {
      console.log(val);
      let _self = this;
      if (val == 1) {
        this.titee = "新增";
        this.dialogFormVisibles = true;
        console.log(val);
      } else {
        console.log(val);
        this.titee = "编辑";
        this.dialogFormVisibles = true;
        _self.abc = val;
        _self.form.nameMe = val.itemText;
        _self.form.nameS = val.itemValue;
        _self.form.nameD = val.description;
        _self.form.num = val.sortOrder;
        _self.form.value = val.status;
      }
    },
    // 字典配置确定新增
    submit(formName) {
      if (this.titee == "新增") {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.dialog();
            this.dicitemListTab();
            (this.form.nameMe = ""),
              (this.form.nameS = ""),
              (this.form.nameD = "");
            this.dialogFormVisibles = false;
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.EditorS();
            this.dicitemListTab();
            this.dialogFormVisibles = false;
          }
        });
      }
    },
    // 编辑接口
    EditorS() {
      let _self = this;
      let data = {
        createBy: _self.abc.createBy,
        createTime: _self.abc.createTime,
        id: _self.abc.id,
        itemText: _self.form.nameMe,
        itemValue: _self.form.nameS,
        description: _self.form.nameD,
        dictId: _self.addId,
        sortOrder: _self.form.num,
        status: Number(_self.form.value),
      };

      // let data ={
      // createBy:"admin",
      // createTime:"2022-10-14 14:12:52",
      // itemText: _self.form.nameMe,
      // itemValue: _self.form.nameS,
      // description: _self.form.nameD,
      // dictId: _self.addId,
      // sortOrder: _self.form.num,
      // status: Number(_self.form.value),
      // }
      Editor(data).then((res) => {
        if (res.code == 200) {
          _self.$message({
            showClose: true,
            message: res.message,
            type: "success",
          });
        }
      });
    },

    //新增弹框
    dialog() {
      let _self = this;
      _self.dialogFormVisibles = true;
      if (_self.form.value) {
        _self.form.value = 1;
      } else {
        _self.form.value = 0;
      }
      let data = {
        itemText: _self.form.nameMe,
        itemValue: _self.form.nameS,
        description: _self.form.nameD,
        dictId: _self.addId,
        sortOrder: _self.form.num,
        status: _self.form.value,
      };
      console.log(data);
      newlyIncreased(data).then((res) => {
        console.log(res);
        _self.$message({
          message: res.message,
          type: "success",
        });
      });
    },
    // 字典配置重置
    reset() {
      let _self = this;
      _self.dataList.itemText = "";
      // console.log(_self.operation);
      _self.dicitemListTab();
    },
    // 字典配置查询
    seek() {
      let _self = this;

      _self.dicitemListTab();
    },
    // 字典配置列表数据总数
    handleSize(val) {
      this.firstsize = val;
      this.dicitemListTab();
    },
    // 字典配置列表第几页
    handleCurrent(val) {
      this.firstNO = val;
      this.dicitemListTab();
    },
    //字典配置点击事件
    keep(val) {
      let _self = this;
      _self.addId = val.id;
      if ((_self.drawer = true)) {
        _self.dicitemListTab();
      }
    },
    // 字典配置数据列表
    dicitemListTab() {
      let _self = this;
      let data = {
        dictId: _self.addId,
        pageNo: _self.firstNO,
        pageSize: _self.firstsize,
        itemText: _self.dataList.itemText,
        status: _self.dataList.itemValue,
        // status:_self.secondformInline.state
      };
      dicitemList(data).then((res) => {
        console.log(res);
        let { records } = res.result;
        if (res.code == 0) {
          _self.tableDataA = [];
          for (let item of records) {
            _self.tableDataA.push(item);
          }
        }
      });
    },

    // 回收站
    dialogTab() {
      let _self = this;
      _self.dialogTableVisible = true;
    },

    // 删除接口函数
    deletesList(operationData) {
      console.log(operationData);
      let _self = this;
      let id = operationData.id;
      deletes(id).then((res) => {
        console.log(res);
        if (res.code == 200) {
          _self.categoryListTreeSearch();
          _self.$refs.recycle.recycleBSearch();
        }
      });
    },

    // 编辑确定
    compileS(formName) {
      let _self = this;
      if (this.edit_OR_add == 2) {
        _self.$refs[formName].validate(async (valid) => {
          if (valid) {
            this.operationData1.dictName = this.ruleForm.Lname;
            this.operationData1.dictCode = this.ruleForm.names;
            this.operationData1.description = this.ruleForm.namew;
            await _self.CompileO();
            await _self.categoryListTreeSearch();
            _self.dialogFormVisible = false;
            _self.ruleForm.namew = "";
            _self.ruleForm.Lname = "";
            _self.ruleForm.names = "";
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else {
        let _self = this;
        _self.$refs[formName].validate((valid) => {
          if (valid) {
            _self.additionSearch();
            _self.categoryListTreeSearch();
            _self.dialogFormVisible = false;
            _self.ruleForm.namew = "";
            _self.ruleForm.Lname = "";
            _self.ruleForm.names = "";
          } else {
            return false;
          }
        });
      }
    },
    CompileO() {
      let _self = this;
      let data = this.operationData1;
      compile(data).then((res) => {
        console.log(res, "compile");
        if (res.code == 200) {
          _self.$message({
            showClose: true,
            message: res.message,
            type: "success",
          });
        }
      });
    },

    // 编辑接口函数
    editData(val) {
      this.dialogFormVisible = true;
      console.log(this.dialogFormVisible);
      if (val == 1) {
        this.ruleForm.Lname = "";
        this.ruleForm.names = "";
        this.ruleForm.namew = "";
        this.operationData1 = "";
        this.edit_OR_add = 1; //添加是1
      } else {
        this.ruleForm.Lname = val.dictName;
        this.ruleForm.names = val.dictCode;
        this.ruleForm.namew = val.description;
        this.operationData1 = val;
        this.edit_OR_add = 2; //编辑是2
      }
    },

    // 添加确定按钮
    add(formName) {
      let _self = this;
      _self.$refs[formName].validate((valid) => {
        if (valid) {
          _self.additionSearch();
          _self.categoryListTreeSearch();
          _self.dialogFormVisible = false;
          _self.ruleForm.namew = "";
          _self.ruleForm.Lname = "";
          _self.ruleForm.names = "";
        } else {
          return false;
        }
      });
    },
    // 重置
    soSubmits() {
      let _self = this;
      _self.formInline.user = "";
      _self.formInline.region = "";
      _self.categoryListTreeSearch();
    },
    // 搜索
    onSubmit() {
      let _self = this;
      _self.pageNo = 1;
      _self.categoryListTreeSearch();
    },
    // 列表数据总数
    handleSizeChange(val) {
      this.pageSize = val;
      this.categoryListTreeSearch();
    },
    // 列表数据第几页
    handleCurrentChange(val) {
      this.pageNo = val;
      this.categoryListTreeSearch();
    },
    // 列表数据
    categoryListTreeSearch() {
      let _self = this;
      _self.tableData = [];
      let data = {
        pageNo: _self.pageNo,
        pageSize: _self.pageSize,
        dictName: _self.formInline.user,
        dictCode: _self.formInline.region,
      };
      console.log(data);
      categoryListTree(data).then((res) => {
        console.log(res);
        let list = res.result.records;
        console.log(list);
        if (res.code == 0) {
          for (let item of list) {
            // console.log("////// item:",item)
            // _self.tableData.push({
            //   ID: item.id,
            //   Lettername: item.dictName,
            //   Alphacode: item.dictCode,
            //   Describe: item.description,
            //   label: item.dictName,
            // });
            _self.tableData.push(item);
          }
          _self.total = res.result.total;
        }
      });
    },
    // 添加
    additionSearch() {
      let _self = this;
      console.log("这是添加");
      if (_self.edit_OR_add == 1) {
        let data = {
          description: _self.ruleForm.namew,
          dictCode: _self.ruleForm.Lname,
          dictName: _self.ruleForm.names,
        };
        console.log(data);
        addition(data).then((res) => {
          console.log(res);
          if (res.code == 200) {
            _self.$message({
              message: res.message,
              type: "success",
            });
          } else {
            _self.message = res.message;
            // console.log(res);
          }
        });
      }
    },
  },
  components: {
    stateTable,
    recycle,
  },
  mounted() {
    this.categoryListTreeSearch();
  },
};
</script>
<style>
.borders {
  border: 1px solid #e9e9e9;
  margin: 20px;
  padding: 10px;
}
.bor {
  border-top: 2px solid #e9e9e9;
}
.white {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
}
.el-scrollbar__thumb {
  display: none;
}
::-webkit-scrollbar {
  width: 1px;
  height: 1px;
}
/* // 滚动条的滑块 */
::-webkit-scrollbar-thumb {
  background-color: #000000;
  border-radius: 0px;
}
</style>